using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LicenseManager;

namespace ICP_MS.Plugin.iMass;

public class ClientMS
{
	private Dictionary<string, string> DicTableData = new Dictionary<string, string>();

	private Dictionary<string, string> DicTableInfoData = new Dictionary<string, string>();

	private Dictionary<string, string> DicTableDataStart = new Dictionary<string, string>();

	private Dictionary<string, string> DicTableDataError = new Dictionary<string, string>();

	private Dictionary<string, string> DicTableReceiveData = new Dictionary<string, string>();

	public Queue<string> data = new Queue<string>();

	private string MonitorData = "";

	private string errorData = "";

	public string infoData = "";

	public int time = 5;

	public object obj = new object();

	public void SetReceiveValueInit()
	{
		DicTableReceiveData.Clear();
		DicTableReceiveData.Add("LEN", "");
		DicTableReceiveData.Add("IST", "");
		DicTableReceiveData.Add("ID", "");
		DicTableReceiveData.Add("CMD", "");
		DicTableReceiveData.Add("DT", "");
		DicTableReceiveData.Add("SIF", "");
		DicTableReceiveData.Add("SPNR", "");
		DicTableReceiveData.Add("SPAY", "");
	}

	public void SetInitStart()
	{
		DicTableDataStart.Clear();
		DicTableDataStart.Add("IST", "");
		DicTableDataStart.Add("ID", "");
		DicTableDataStart.Add("DT", "");
	}

	public void SetInitInfo()
	{
		DicTableInfoData.Clear();
		DicTableDataStart.Add("CMD", "I");
		DicTableInfoData.Add("PNR", "");
		DicTableInfoData.Add("PAY", "");
		DicTableInfoData.Add("PCL", "");
		DicTableInfoData.Add("IPT", "");
		DicTableInfoData.Add("PSCW", "");
		DicTableInfoData.Add("PSLT", "");
		DicTableInfoData.Add("PSCS", "");
		DicTableInfoData.Add("PSAS", "");
		DicTableInfoData.Add("PSDR", "");
		DicTableInfoData.Add("PSPS", "");
		DicTableInfoData.Add("PSCK", "");
		DicTableInfoData.Add("PSP24", "");
		DicTableInfoData.Add("PSN24", "");
		DicTableInfoData.Add("PSCIL", "");
		DicTableInfoData.Add("PRN", "");
		DicTableInfoData.Add("PWR", "");
		DicTableInfoData.Add("PFD", "");
		DicTableInfoData.Add("PTW", "");
		DicTableInfoData.Add("PRFF", "");
		DicTableInfoData.Add("PITE", "");
		DicTableInfoData.Add("PSLO", "");
		DicTableInfoData.Add("PEWB", "");
		DicTableInfoData.Add("PEII", "");
		DicTableInfoData.Add("PEMB", "");
		DicTableInfoData.Add("PEPA", "");
		DicTableInfoData.Add("PEEI", "");
		DicTableInfoData.Add("PETO", "");
		DicTableInfoData.Add("PEQU", "");
		DicTableInfoData.Add("T1FQ", "");
		DicTableInfoData.Add("T1TE", "");
		DicTableInfoData.Add("T1VT", "");
		DicTableInfoData.Add("T1CT", "");
		DicTableInfoData.Add("T1BG", "");
		DicTableInfoData.Add("T2FQ", "");
		DicTableInfoData.Add("T2TE", "");
		DicTableInfoData.Add("T2VT", "");
		DicTableInfoData.Add("T2CT", "");
		DicTableInfoData.Add("T2BG", "");
		DicTableInfoData.Add("VMQD", "");
		DicTableInfoData.Add("VMIE", "");
		DicTableInfoData.Add("VMLS", "");
		DicTableInfoData.Add("VER", "");
		DicTableInfoData.Add("QTE", "");
		DicTableInfoData.Add("QPH", "");
		DicTableInfoData.Add("QLV", "");
		DicTableInfoData.Add("QRF", "");
		DicTableInfoData.Add("QDC", "");
		DicTableInfoData.Add("QHV", "");
		DicTableInfoData.Add("QI48", "");
		DicTableInfoData.Add("QFQ", "");
		DicTableInfoData.Add("CCN", "");
		DicTableInfoData.Add("CPM", "");
		DicTableInfoData.Add("CICP", "");
		DicTableInfoData.Add("CVM", "");
		DicTableInfoData.Add("CPC", "");
		DicTableInfoData.Add("CMFC", "");
		DicTableInfoData.Add("CLS", "");
		DicTableInfoData.Add("CPR", "");
		DicTableInfoData.Add("CQD", "");
		DicTableInfoData.Add("PER", "");
	}

	public void SetInitError()
	{
		DicTableDataError.Clear();
		DicTableDataError.Add("CMD", "E");
		DicTableDataError.Add("VER", "");
	}

	public void SetStartValue()
	{
		DicTableDataStart["IST"] = "0";
		DicTableDataStart["ID"] = EncryptManager.GetInstrumentCode();
		DicTableDataStart["DT"] = DateTime.Now.ToString("yyyyMMddHHmm");
	}

	public void SetInfoValue()
	{
		DicTableInfoData["PNR"] = PlasmaManager.plasma.ReadNebuliser.ToString("F2");
		DicTableInfoData["PAY"] = PlasmaManager.plasma.ReadAuxiliary.ToString("F2");
		DicTableInfoData["PCL"] = PlasmaManager.plasma.ReadCool.ToString("F2");
		DicTableInfoData["IPT"] = (PlatManager.Instrumentinjector.Perist / 10).ToString("F2");
		DicTableInfoData["PSCW"] = ((PlasmaManager.plasma.StatusCoolWater == 0) ? "T" : "F");
		DicTableInfoData["PSLT"] = ((PlasmaManager.plasma.StatusLight == 0) ? "T" : "F");
		DicTableInfoData["PSCS"] = ((PlasmaManager.plasma.StatusCoolSwitch == 0) ? "T" : "F");
		DicTableInfoData["PSAS"] = ((PlasmaManager.plasma.StatusArSwitch == 0) ? "T" : "F");
		DicTableInfoData["PSDR"] = ((PlasmaManager.plasma.StatusDoor == 0) ? "T" : "F");
		DicTableInfoData["PSPS"] = ((PlasmaManager.plasma.StatusPowerSwitch == 0) ? "T" : "F");
		DicTableInfoData["PSCK"] = ((PlasmaManager.plasma.StatusCanAndGasFlowInLock == 0) ? "T" : "F");
		DicTableInfoData["PSP24"] = ((PlasmaManager.plasma.StatusPositive24 == 0) ? "T" : "F");
		DicTableInfoData["PSN24"] = ((PlasmaManager.plasma.StatusNegative24 == 0) ? "T" : "F");
		DicTableInfoData["PSCIL"] = ((PlasmaManager.plasma.StatusCLRIntLock == 0) ? "T" : "F");
		DicTableInfoData["PRN"] = PlasmaManager.plasma.ReadReflection.ToString("F1");
		DicTableInfoData["PWR"] = PlasmaManager.plasma.ReadWater.ToString("F0");
		DicTableInfoData["PFD"] = PlasmaManager.plasma.ReadForward.ToString("F2");
		DicTableInfoData["PTW"] = PlasmaManager.plasma.ReadTemperatureWind.ToString("F2");
		DicTableInfoData["PRFF"] = ((PlasmaManager.plasma.RFFailure == 0) ? "T" : "F");
		DicTableInfoData["PITE"] = PlasmaManager.plasma.ReadInterfaceTemperature.ToString("F2");
		DicTableInfoData["PSLO"] = ((PlasmaManager.plasma.StatusLightPrOn == 0) ? "T" : "F");
		byte err = (byte)PlasmaManager.plasma.ReadError;
		DicTableInfoData["PEWB"] = (((err & 4) >> 2 == 1) ? "F" : "T");
		DicTableInfoData["PEII"] = (((err & 2) >> 1 == 1) ? "F" : "T");
		DicTableInfoData["PEMB"] = (((err & 8) >> 3 == 1) ? "F" : "T");
		DicTableInfoData["PEPA"] = (((err & 0x10) >> 4 == 1) ? "F" : "T");
		DicTableInfoData["PEEI"] = (((err & 0x40) >> 6 == 1) ? "F" : "T");
		DicTableInfoData["PETO"] = (((err & 0x20) >> 5 == 1) ? "F" : "T");
		DicTableInfoData["PEQU"] = (((err & 0x80) >> 7 == 1) ? "F" : "T");
		DicTableInfoData["T1FQ"] = Vacuum.turbo1.PZD2.ToString();
		DicTableInfoData["T1TE"] = Vacuum.turbo1.PZD3.ToString();
		DicTableInfoData["T1VT"] = Vacuum.turbo1.PZD6.ToString();
		DicTableInfoData["T1CT"] = Vacuum.turbo1.PZD4.ToString();
		DicTableInfoData["T1BG"] = Vacuum.turbo1.PZD5.ToString();
		DicTableInfoData["T2FQ"] = Vacuum.turbo2.PZD2.ToString();
		DicTableInfoData["T2TE"] = Vacuum.turbo2.PZD3.ToString();
		DicTableInfoData["T2VT"] = Vacuum.turbo2.PZD6.ToString();
		DicTableInfoData["T2CT"] = Vacuum.turbo2.PZD4.ToString();
		DicTableInfoData["T2BG"] = Vacuum.turbo2.PZD5.ToString();
		DicTableInfoData["VMQD"] = Vacuum.vacumm.ReadVacuum[2].ToString();
		DicTableInfoData["VMIE"] = Vacuum.vacumm.ReadVacuum[0].ToString("F2");
		DicTableInfoData["VMLS"] = Vacuum.vacumm.ReadVacuum[1].ToString("F3");
		DicTableInfoData["QTE"] = QuadMonitor.quad.Temperature.ToString();
		DicTableInfoData["QPH"] = QuadMonitor.quad.ReadPheat.ToString("F2");
		DicTableInfoData["QLV"] = QuadMonitor.quad.ReadLV.ToString("F2");
		DicTableInfoData["QRF"] = (double.IsInfinity(QuadMonitor.quad.ReadRF) ? "0" : QuadMonitor.quad.ReadRF.ToString("F2"));
		DicTableInfoData["QDC"] = QuadMonitor.quad.ReadDC.ToString("F2");
		DicTableInfoData["QHV"] = QuadMonitor.quad.ReadHV.ToString("F2");
		DicTableInfoData["QI48"] = QuadMonitor.quad.ReadI48.ToString("F2");
		DicTableInfoData["QFQ"] = QuadMonitor.quad.ReadFreq.ToString();
		DicTableInfoData["CCN"] = (Collision.collision.StatusConnected ? "T" : "F");
		DicTableInfoData["CPM"] = (PlatManager.plat.StatusConnected ? "T" : "F");
		DicTableInfoData["CICP"] = (PlasmaManager.plasma.StatusConnected ? "T" : "F");
		DicTableInfoData["CVM"] = (Vacuum.vacumm.StatusConnected ? "T" : "F");
		DicTableInfoData["CPC"] = (PlatManager.injector.StatusConnected ? "T" : "F");
		DicTableInfoData["CMFC"] = (PlasmaManager.mfcStatus.StatusConnected ? "T" : "F");
		DicTableInfoData["CLS"] = (LensManager.x2lens.StatusConnected ? "T" : "F");
		DicTableInfoData["CPR"] = (PowerManager.powerStatus.StatusConnected ? "T" : "F");
		DicTableInfoData["CQD"] = (QuadMonitor.quad.StatusConnected ? "T" : "F");
	}

	public void SetErrorValue()
	{
		DicTableDataError["VER"] = Vacuum.lastWarnning.WarnState.ToString();
	}

	public void ToStr(string cmd)
	{
		MonitorData = "";
		MonitorData += "@@;";
		string data = "";
		foreach (KeyValuePair<string, string> dic3 in DicTableDataStart)
		{
			string keystring3 = dic3.Key;
			string valuestring3 = dic3.Value;
			string dicdata3 = keystring3 + "=" + valuestring3 + ";";
			data += dicdata3;
		}
		if (cmd == "I")
		{
			foreach (KeyValuePair<string, string> dic in DicTableInfoData)
			{
				string keystring = dic.Key;
				string valuestring = dic.Value;
				string dicdata = keystring + "=" + valuestring + ";";
				data += dicdata;
			}
		}
		else if (cmd == "E")
		{
			foreach (KeyValuePair<string, string> dic2 in DicTableDataError)
			{
				string keystring2 = dic2.Key;
				string valuestring2 = dic2.Value;
				if (valuestring2 != "")
				{
					string dicdata2 = keystring2 + "=" + valuestring2 + ";";
					data += dicdata2;
				}
			}
		}
		string strlen = "LEN=" + Encoding.UTF8.GetByteCount(data) + ";";
		MonitorData += strlen;
		MonitorData += data;
		MonitorData += "##";
		if (cmd == "E")
		{
			errorData = MonitorData;
		}
		else if (cmd == "I")
		{
			infoData = MonitorData;
		}
	}

	public void Init()
	{
		ClientLIMS.Receiveevent += ProcessData;
	}

	private void ProcessData(string data)
	{
		SetReceiveValueInit();
		if (!data.StartsWith("@@") || !data.EndsWith("##"))
		{
			return;
		}
		string[] ss = data.Split(';');
		if (ss != null && ss.Count() > 0)
		{
			for (int i = 0; i < ss.Count(); i++)
			{
				string[] splitss = ss[i].Split('=');
				if (splitss.Count() == 2)
				{
					switch (splitss[0])
					{
					case "LEN":
						DicTableReceiveData["LEN"] = splitss[1];
						break;
					case "IST":
						DicTableReceiveData["IST"] = splitss[1];
						break;
					case "ID":
						DicTableReceiveData["ID"] = splitss[1];
						break;
					case "CMD":
						DicTableReceiveData["CMD"] = splitss[1];
						break;
					case "DT":
						DicTableReceiveData["DT"] = splitss[1];
						break;
					case "SIF":
						DicTableReceiveData["SIF"] = splitss[1];
						break;
					case "SPNR":
						DicTableReceiveData["SPNR"] = splitss[1];
						break;
					case "SPAY":
						DicTableReceiveData["SPAY"] = splitss[1];
						break;
					}
				}
			}
		}
		if (DicTableReceiveData["LEN"] == data.Length.ToString() && DicTableReceiveData["IST"] == "0" && DicTableReceiveData["ID"] == "10000001" && DicTableReceiveData["CMD"] == "R")
		{
			if (DicTableReceiveData["SIF"] != "")
			{
				int freq = Convert.ToInt32(DicTableReceiveData["SIF"]);
				time = freq;
			}
			if (DicTableReceiveData["SPNR"] != "")
			{
				double Nebuliser2 = Convert.ToDouble(DicTableReceiveData["SPNR"]);
			}
			if (DicTableReceiveData["SPAY"] != "")
			{
				double Nebuliser = Convert.ToDouble(DicTableReceiveData["SPAY"]);
			}
		}
	}

	public void SetErrorMessage()
	{
		lock (obj)
		{
			SetInitStart();
			SetInitError();
			SetStartValue();
			SetErrorValue();
			ToStr("E");
			data.Enqueue(errorData);
		}
	}

	public void SetInfoMessage()
	{
		lock (obj)
		{
			SetInitStart();
			SetInitInfo();
			SetStartValue();
			SetInfoValue();
			ToStr("I");
		}
	}
}
